<?php
/**
 * Created by PhpStorm.
 * User: xcx
 * Date: 2017/6/2
 * Time: 10:17
 */

class Controller_Adminmall_User extends Controller_Adminmall_Abstract
{
    private $DB_SERVER = "xhs_slave";

    /*
     * 用户列表
     */
    public function listAction(){
//        $this->openDebug();
        $page = FRequest::getInt("page");
        $uid = FRequest::getInt('uid');
        $cell_phone = FRequest::getString("mobile") ? FRequest::getString("mobile") : "";
        $username = FRequest::getString("username") ? FRequest::getString("username") : "";
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['u.create_time']['gte'] = $start_time.' 00:00:00';
        $where['u.create_time']['lte'] = $end_time.' 23:59:59';

        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);

        if($uid){
            $where['u.uid'] = $uid;
        }

        if($username){
            $where['u.username'] = $username;
        }

        if($cell_phone){
            $where['u.cell_phone'] = $cell_phone;
        }

        $where['status'] = 1;

        $userTable = new FTable("user", 'u', $this->DB_SERVER);
        $list = $userTable->fields("u.*")
            ->where($where)
            ->order("uid desc")
            ->limit(20)
            ->page($page)
            ->select();

        $pagerInfo = $userTable->getPagerInfo();
        $this->assign('page_info', $pagerInfo);

        $this->assign("data", $list);
        $this->display('user/user-list');




    }

    /*
     * 用户状态修改
     */
    public function modifyAction(){
        global $_F;
        $uid = FRequest::getInt("uid");
        if($this->isPost()){
            $uid = FRequest::getPostInt('uid');
            $username = FRequest::getPostString("username");
            $cell_phone = FRequest::getPostString("cell_phone");
            $status = FRequest::getPostInt("status");

            $save = array();
            if($username){
                $save['username'] = $username;
            }
            if($cell_phone){
                $save['cell_phone'] = $cell_phone;
            }

            $save['status'] = $status;
            $table = new FTable("user", "", 'xhs_master');
            $table->update1($save, array("uid" => $uid));
            $this->success("修改成功", $_F['uri'] . "?uid=" . $uid);
        }else{
            $table =  new FTable("user", "", $this->DB_SERVER);
            $data = $table->fields("*")->where(array("uid" => $uid))->find();
            $this->assign("data", $data);
        }
        $this->display("user/modify");
    }

    /*
     * 删除用户
     */
    public function delAction(){

    }


    public function modifyStatusAction(){
        global $_F;
        $flag = FRequest::getInt("flag") ? FRequest::getInt("flag") : 0;
        $uid = FRequest::getInt("uid");
        $status = FRequest::getInt("status");
        if ($uid) {
            $table = new FTable("user", "", 'xhs_master');
            $table->update1(array('status' => $status), array("uid" => $uid));
            $this->success("操作成功!", $_F['refer']);
        } else {
            $this->success("操作失败，id为空！", $_F['refer']);
        }
    }


    /*
     * 连锁店统计列表
     */
    public function tjAction(){
        global $_F;
//        $this->openDebug();
        $page = FRequest::getInt("page");
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['create_time']['gte'] = $start_time.' 00:00:00';
        $where['create_time']['lte'] = $end_time.' 23:59:59';
        $shop_id = $_F['member']['shop_id'];
        $where['shop_id'] = $shop_id;

        $where['status'] = 5;

        $userTable = new FTable("orders", '', $this->DB_SERVER);
        $list = $userTable->fields("*")
            ->where($where)
            ->order("auto_id desc")
            ->limit(20)
            ->page($page)
            ->select();

        $pagerInfo = $userTable->getPagerInfo();
        $this->assign('page_info', $pagerInfo);
        $this->assign("data", $list);

        $tongji = Service_User::incomeTjAction($shop_id);
        $this->assign("tongji", $tongji);
//        var_dump($tongji);
        $chainTable = new FTable("chain_shop", "", $this->DB_SERVER);
        $chainData = $chainTable->where("status=1")->select();
        $this->assign("chainData", $chainData);



        $this->display("user/tj-list");
    }

    /*
     * 订单统计列表
     */
    public function tjAdminAction(){
        global $_F;
//        $this->openDebug();
        $page = FRequest::getInt("page");
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');
        $phone = FRequest::getString('phone');
        $shop_id = FRequest::getInt("shop_id");

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['create_time']['gte'] = $start_time.' 00:00:00';
        $where['create_time']['lte'] = $end_time.' 23:59:59';

        if($shop_id){
            $where['shop_id'] = $shop_id;
        }

        if($phone){
            $where['phone'] = $phone;
        }


        $where['status'] = 5;

        $userTable = new FTable("orders", '', $this->DB_SERVER);
        $list = $userTable->fields("*")
            ->where($where)
            ->order("auto_id desc")
            ->limit(20)
            ->page($page)
            ->select();

        $productTable = new FTable("orders_product", "", $this->DB_SERVER);
        foreach ($list as &$item) {
            if ($item['o_id']) {
                $ret = $productTable->fields("sum(price_old) as price_old_zong, sum(paynum*purchase_price) as purchase_price_sum")->where("o_id={$item['o_id']}")->find();
                $item['price_old_zong'] = $ret['price_old_zong'];
                $item['purchase_price_sum'] = $ret['purchase_price_sum'];
            }else{
                $item['price_old_zong'] = 0;
            }
        }

        $pagerInfo = $userTable->getPagerInfo();
        $this->assign('page_info', $pagerInfo);
        $this->assign("data", $list);

        $tongji = Service_User::incomeTjAction($shop_id);
        $this->assign("tongji", $tongji);
//        var_dump($tongji);

        $chainTable = new FTable("chain_shop", "", $this->DB_SERVER);
        $chainData = $chainTable->where("status=1")->select();
        $this->assign("chainData", $chainData);
        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);

        $this->display("user/tj-list-admin");
    }

    /**
     * 订单统计分析导出功能
     */
    public function orderTjDaoAction(){
        global $_F;
        $this->openDebug();
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');
        $phone = FRequest::getString('phone');
        $shop_id = FRequest::getInt("shop_id");

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['o.create_time']['gte'] = $start_time.' 00:00:00';
        $where['o.create_time']['lte'] = $end_time.' 23:59:59';

        if($shop_id){
            $where['o.shop_id'] = $shop_id;
        }

        if($phone){
            $where['o.phone'] = $phone;
        }


        $where['o.status'] = 5;

        var_dump($where);
        exit();

        $userTable = new FTable("orders", 'o', $this->DB_SERVER);
        $list = $userTable->fields("*, cs.name as shop_name")
            ->leftJoin("chain_shop", "cs", "cs.id=o.shop_id")
            ->where($where)
            ->select();

        $productTable = new FTable("orders_product", "", $this->DB_SERVER);
        foreach ($list as &$item) {
            if ($item['o_id']) {
                $ret = $productTable->fields("sum(price_old) as price_old_zong")->where("o_id={$item['o_id']}")->find();
                $item['price_old_zong'] = $ret['price_old_zong'];
            }else{
                $item['price_old_zong'] = 0;
            }
        }
//
        $data = array();
        $data[0] = array("日期", "订单号", "收货人", "联系方式", "商品金额",
            "配送费", "总计金额", "所属连锁店", "接单耗时", "制作耗时", "配送耗时", "总耗时", "配送距离km",
            "商品结算成本", "本单配送成本", "本单利润");

        foreach ($list as $k => $o) {
            $data[$k + 1][0] = date("m-d", strtotime($o['create_time']));
            $data[$k + 1][1] = $o['o_id'];
            $data[$k + 1][2] = $o['contact'];
            $data[$k + 1][3] = $o['phone'];
            $data[$k + 1][4] = $o['price'];
            $data[$k + 1][5] = $o['send_price'];
            $data[$k + 1][6] = $o['pay_price'];
            $data[$k + 1][7] = $o['shop_name'];
            $data[$k + 1][8] = $o['recv_expend'];
            $data[$k + 1][9] = $o['make_expend'];
            $data[$k + 1][10] = $o['send_expend'];
            $data[$k + 1][11] = $o['recv_expend'] + $o['make_expend'] + $o['send_expend'];
            $data[$k + 1][10] = $o['dis'];
            $data[$k + 1][10] = $o['price_old_zong'];
            $data[$k + 1][10] = $o['real_send_price'];
            $data[$k + 1][10] = $o['profit'];
        }

        $sheetName = "{$_GET['begin_date']}至{$_GET['end_date']}";
        $fileName = iconv("utf-8", "gb2312", "{$sheetName}订单列表导出");
        $xls = new FExcel('UTF-8', false, $sheetName);
        $xls->addArray($data);
        $xls->generateXML($fileName);


    }

    /**
     * 连锁店统计
     */
    public function shopTjAction(){
        global $_F;
//        $this->openDebug();
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');
        $shop_id = FRequest::getInt("shop_id");


        //连锁店统计
        $where = array();
        if($shop_id){
            $where['shop_id'] = $shop_id;
        }
        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-2 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $where['status'] = 5;
        $userTable = new FTable("orders", '', $this->DB_SERVER);
        $data = array();
        if($end_time < $start_time){
            $this->error("选择的时间不对", $_F['refer']);
        }
        $start_time_tm = $start_time;
//        $end_time = "2017-08-10";


        while(true){

            $where['create_time']['gte'] = $start_time_tm.' 00:00:00';
            $where['create_time']['lte'] = $start_time_tm.' 23:59:59';
            if(!$shop_id){
                $shopTable = new FTable("chain_shop", "", $this->DB_SERVER);
                $shopData = $shopTable->select();
                $data_tmp = array();
                $where_shop = $where;
                foreach($shopData as $k => $v){
                    $where_shop['shop_id'] = $v['id'];
                    $ret = $userTable->fields("shop_id, sum(price) as price_sum, sum(send_price) as send_price_sum,
            sum(pay_price) as pay_price_sum, sum(paynum*purchase_price) as purchase_price_sum,
            sum(real_send_price) as real_send_price_sum, sum(profit) as profit_sum,
            sum(youhui_price) as youhui_price_sum,
            count(auto_id) as count_sum, avg(recv_expend) as recv_expend_sum,
            avg(make_expend) as make_expend_sum, avg(send_expend) as send_expend_sum,
            avg(dis) as dis_sum")->where($where_shop)->select();
                    if($ret){
                        $data_tmp[$v['id']] = $ret;
                    }
                }
                if($data_tmp){
                    $data[$start_time_tm] = $data_tmp;
                }
            }else{
                $ret = $userTable->fields("shop_id, sum(price) as price_sum, sum(send_price) as send_price_sum,
            sum(pay_price) as pay_price_sum, sum(paynum*purchase_price) as purchase_price_sum,
            sum(real_send_price) as real_send_price_sum, sum(profit) as profit_sum,
            sum(youhui_price) as youhui_price_sum,
            count(auto_id) as count_sum, avg(recv_expend) as recv_expend_sum,
            avg(make_expend) as make_expend_sum, avg(send_expend) as send_expend_sum,
            avg(dis) as dis_sum")->where($where)->select();
                if($ret){
                    $data_tmp[$shop_id] = $ret;
                    $data[$start_time_tm] = $data_tmp;
                }
            }

            $tm = strtotime($start_time_tm) + 86400;
            $start_time_tm = date("Y-m-d", $tm);
            if($start_time_tm>$end_time){
                break;
            }
        }
//        var_dump($where);
//        FResponse::output($data);
//        exit();



        //汇总
        $whereT = array();
        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }
        $whereT['create_time']['gte'] = $start_time.' 00:00:00';
        $whereT['create_time']['lte'] = $end_time.' 23:59:59';

        if($shop_id){
            $whereT['shop_id'] = $shop_id;
        }
        $whereT['status'] = 5;
        $userTable = new FTable("orders", '', $this->DB_SERVER);
        $huizong = $userTable->fields("sum(price) as price_sum, sum(send_price) as send_price_sum,
            sum(pay_price) as pay_price_sum, sum(paynum*purchase_price) as purchase_price_sum,
            sum(real_send_price) as real_send_price_sum, sum(profit) as profit_sum,
            sum(youhui_price) as youhui_price_sum,
            count(auto_id) as count_sum, avg(recv_expend) as recv_expend_sum,
            avg(make_expend) as make_expend_sum, avg(send_expend) as send_expend_sum,
            avg(dis) as dis_sum")
            ->where($whereT)
            ->find();

//        var_dump($huizong);
//        FResponse::output($huizong);
//        exit();


        $this->assign("huizong", $huizong);

        $tongji = Service_User::incomeTjAction($shop_id);
        $this->assign("tongji", $tongji);
//        var_dump($tongji);

        $chainTable = new FTable("chain_shop", "", $this->DB_SERVER);
        $chainData = $chainTable->where("status=1")->select();
        $this->assign("chainData", $chainData);
        $this->assign("data", $data);
//        print_r($data);
        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);
        $this->display("user/tj-shop");
    }

    public function testAction(){
        $this->openDebug();
//        echo "3333";
        $t = FRequest::getInt('t');
        $a = Service_Order::timeFormat($t);
         var_dump($a);
        exit();

        $id = FRequest::getInt('id');
        $table = new FTable('orders', '', 'xhs_master');
        if($id){
            $ret = $table->where(array('o_id' => $id))->save(array('pay_price' => 0.01));

            if($ret){
                $this->success("修改成功");
            }
        }else{
            $this->error('id为空');
        }

        exit();



        for($i=0 ; $i<4; $i++){
            var_dump($i);
        }
        exit();
        $a = "123456";
        var_dump(substr($a, 1));
        exit();

        $t = "a:12:{i:0;s:16:\"is_super_manager\";i:1;s:16:\"/admin/main/edit\";i:2;s:17:\"/admin/main/total\";i:3;s:23:\"/admin/main/totalsearch\";i:4;s:26:\"/admin/main/totalsearchdao\";i:5;s:19:\"/admin/main/monitor\";i:6;s:22:\"/admin/bellegroup/list\";i:7;s:24:\"/admin/bellegroup/modify\";i:8;s:24:\"/admin/bellegroup/delete\";i:9;s:21:\"/admin/manager/delete\";i:10;s:26:\"/admin/manager/groupmodify\";i:11;s:21:\"/admin/actionlog/list\";}";
        $ar =unserialize($t);
        $arr = array(
            0 => "is_super_manager",
            1 => "/User/list",
            2 => "/User/list",
            3 => "/User/list",
            4 => "/User/list",
            5 => "/User/list",
            6 => "/User/list",
            7 => "/User/list",
            8 => "/User/list",
            9 => "/User/list",
        );
        var_dump($ar);
    }


}